Guide to NoSQL with Azure CosmosDB by Gaston C. Hillar

Guide to NoSQL with Azure CosmosDB by Gaston C. Hillar

Author:Gaston C. Hillar [Gaston C. Hillar]
Language: eng
Format: epub
Tags: COM021000 - COMPUTERS / Databases / General, COM060080 - COMPUTERS / Web / General, COM021050 - COMPUTERS / Databases / Servers
Publisher: Packt
Published: 2018-09-28T06:35:51+00:00


Querying and creating document collections

The following lines declare the code for the CreateCollectionIfNotExistsAsync asynchronous static method, which creates a new document collection if a collection with id equal to the value stored in the collectionId field doesn't exist in the database. Add the following lines to the existing code of the Program.cs file. The code file for the sample is included in the learning_cosmos_db_04_01 folder in the dot_net_core_2_samples/SampleApp1/SampleApp1/Program.cs file:

private static async Task<DocumentCollection> CreateCollectionIfNotExistsAsync() { var databaseUri = UriFactory.CreateDatabaseUri(databaseId); DocumentCollection documentCollectionResource; var isCollectionCreated = await client.CreateDocumentCollectionQuery(databaseUri) .Where(c => c.Id == collectionId) .CountAsync() == 1; if (isCollectionCreated) { Console.WriteLine($"The collection {collectionId} already exists."); var documentCollectionUri = UriFactory.CreateDocumentCollectionUri(databaseId, collectionId); var documentCollectionResponse = await client.ReadDocumentCollectionAsync(documentCollectionUri); documentCollectionResource = documentCollectionResponse.Resource; } else { var documentCollection = new DocumentCollection { Id = collectionId, }; documentCollection.PartitionKey.Paths.Add("/location/zipCode"); var uniqueKey = new UniqueKey(); uniqueKey.Paths.Add("/title"); documentCollection.UniqueKeyPolicy.UniqueKeys.Add(uniqueKey); var requestOptions = new RequestOptions { OfferThroughput = 1000, }; var collectionResponse = await client.CreateDocumentCollectionAsync( databaseUri, documentCollection, requestOptions); if (collectionResponse.StatusCode == System.Net.HttpStatusCode.Created) { Console.WriteLine($"The collection {collectionId} has been created."); } documentCollectionResource = collectionResponse.Resource; } return documentCollectionResource; }

The code doesn't use the easiest mechanism to create a collection when it doesn't exist because we will analyze how we can query the document collections for a database. Our goal is to learn about many possibilities offered by the SDK that will enable us to develop many different kinds of applications that work with Cosmos DB. However, it is very important to notice that the code for this method could be simplified by calling the CreateDocumentCollectionIfNotExistsAsync method.

First, the code calls the UriFactory.CreateDatabaseUri method with databaseId as its argument and saves the result in the databaseUri variable. This method will return a Uri instance with the URI for the database ID received as an argument. The code will use this URI to easily address the database resource in which we have to perform operations.

Note that the UriFactory.CreateDatabaseUri method requires the database ID to build the Uri instance and doesn't require any query to the database. However, of course, we must be sure that the database resource with the specified ID already exists before using the generated URI.

The next line declares the documentCollectionResource variable as a DocumentCollection instance. The code will end up returning this variable.

Then, the code creates a LINQ query with a call to the asynchronous client.CreateDocumentCollectionQuery method with databaseUri as an argument. This counts the number of collections whose Id is equal to collectionId with an asynchronous execution due to the usage of the chained CountAsync method. If the results of this query on the collections for the database is 1, it means that the collection already exists.

The following lines show the code that builds the LINQ query and stores the results of the Boolean expression in the isCollectionCreated variable:

var isCollectionCreated = await client.CreateDocumentCollectionQuery(databaseUri) .Where(c => c.Id == collectionId) .CountAsync() == 1;

We can easily query the existing collections in a document database by chaining LINQ expressions to the call to the CreateDocumentCollectionQuery method. In this case, we are always working with asynchronous methods. If we used the Count method instead of CountAsync, the query would have a synchronous execution.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.